#include "keychooserdelegate.h"
#include "keybutton.h"

KeyChooserDelegate::KeyChooserDelegate(QObject *parent) :
    QStyledItemDelegate(parent)
{
}

QWidget* KeyChooserDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const{
    KeyButton *button = new KeyButton(parent);
    return button;
}
void KeyChooserDelegate::setEditorData (QWidget *editor, const QModelIndex &index) const{
    int value = index.model()->data(index, Qt::EditRole).toInt();

    KeyButton *button = static_cast<KeyButton*>(editor);
    button->SetKey(value);
}

void KeyChooserDelegate::updateEditorGeometry (QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const{
    QRect rect = option.rect;
    editor->setGeometry(rect);
}

void KeyChooserDelegate::setModelData (QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const{
    KeyButton *button = static_cast<KeyButton*>(editor);


    model->setData(index,button->GetKey(),Qt::EditRole);
}

bool KeyChooserDelegate::eventFilter(QObject* editor, QEvent* event)
{
     if(event->type()==QEvent::KeyPress)
          return false;
     return QStyledItemDelegate::eventFilter(editor, event);
}
